package cn.kgc.gmall.controller;

import cn.kgc.gmall.pojo.ProductStats;
import cn.kgc.gmall.pojo.ProvinceStats;
import cn.kgc.gmall.service.PublisherService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.yaml.snakeyaml.events.Event;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController  // 等价于 controller + responseBody    controller: 将类的控制器交给Spring容器 responseBody响应的数据以json字符串的方式
@RequestMapping("/api/sugar")
public class GmallPublisherApp {
    @Autowired
    private PublisherService publisherService;

    // $API_HOST/api/sugar/province
    @RequestMapping("/province")
    public String getProvince(@RequestParam(name = "date",defaultValue = "0") Integer date){
        // 时间范围里面按照天来统计的  将date参数变成每天的查询时间
        if (date == 0){
            date = now();
        }
       List<ProvinceStats> provinceStatsList = publisherService.getProvince(date);
        // 封装json
        StringBuilder jsonBuilder = new StringBuilder("{\"status\":0,\"data\":{\"mapData\":[");

        if (provinceStatsList.size() == 0) {
            //    jsonBuilder.append(  "{\"name\":\"北京\",\"value\":0.00}");
        }
        for (int i = 0; i < provinceStatsList.size(); i++) {
            if (i >= 1) {
                jsonBuilder.append(",");
            }
            ProvinceStats provinceStats = provinceStatsList.get(i);
            jsonBuilder.append("{\"name\":\"" + provinceStats.getProvince_name() + "\",\"value\":" + provinceStats.getOrder_amount() + " }");

        }
        jsonBuilder.append("],\"valueName\": \"交易额\"}}");
        return jsonBuilder.toString();

    }
    // 接受大屏的请求 : /api/sugar/gmv
    // 返回值: {
    //    "status": 0,
    //    "msg": "",
    //    "data": 1201081.1632389291
    //}
    // 每个请求过来都是要查询当天的数据
    @RequestMapping("/gmv")
    public String getGMV(@RequestParam(name = "date",defaultValue = "0") Integer date ){
        // 时间范围里面按照天来统计的  将date参数变成每天的查询时间
        if (date == 0){
            date = now();
        }
        // 查询每日的成交金额
        BigDecimal gmv = publisherService.getGMV(date);
        String json = "{   \"status\": 0,  \"data\":" + gmv + "}";

        return json;
    }

    public Integer now(){
        String date = DateFormatUtils.format(new Date(), "yyyyMMdd");
        return Integer.parseInt(date);
    }

    // $API_HOST/api/sugar/trademark?limit=5
    // $API_HOST/api/sugar/spu?limit=10
    // $API_HOST/api/sugar/category3

    /**
     * /*
     *     {
     *      "status": 0,
     *      "data": {
     *          "categories": [
     *              "三星","vivo","oppo"
     *          ],
     *          "series": [
     *              {
     *                  "data": [ 406333, 709174, 681971
     *                  ]
     *              }
     *          ]
     *      }
     *     }
     *
     * @param limit
     * @return
     */
    @RequestMapping("/trademark")
    public String getTrademark(@RequestParam(name = "date",defaultValue = "0") Integer date,
                               @RequestParam(name = "limit",defaultValue = "4") Integer limit){
        // 初始化时间
        if (date == 0){
            date = now();
        }
        List<ProductStats> productStatsList =  publisherService.getTrademark(date,limit);

        // 封装成json
        List<String> categories = new ArrayList();
        List<BigDecimal> series = new ArrayList();
        for (ProductStats productStats : productStatsList) {
            categories.add(productStats.getTm_name());
            series.add(productStats.getOrder_amount());
        }
        String json = "{\"status\":0,\"data\":{" + "\"categories\":" +
                "[\"" + StringUtils.join(categories, "\",\"") + "\"],\"series\":[" +
                "{\"data\":[" + StringUtils.join(series, ",") + "]}]}}";



        return json;
    }

    /*
        {
            "status": 0,
            "data": {
                "columns": [
                    { "name": "商品名称",  "id": "spu_name"
                    },
                    { "name": "交易额", "id": "order_amount"
                    }
                ],
                "rows": [
                    {
                        "spu_name": "小米10",
                        "order_amount": "863399.00"
                        "order_count":"2323"
                    },
                   {
                        "spu_name": "iPhone11",
                        "order_amount": "548399.00"
                    }
                ]
            }
        }
    */
    @RequestMapping("/spu")
    public String getSPU(@RequestParam(name = "date",defaultValue = "0") Integer date,
                         @RequestParam(name = "date",defaultValue = "10") Integer limit){
        if (date == 0){
            date = now();
        }
        List<ProductStats> productStatsList = publisherService.getSPU(date,limit);
        // 封装json头部
        StringBuilder jsonBuilder =
                new StringBuilder(" " +
                        "{\"status\":0,\"data\":{\"columns\":[" +
                        "{\"name\":\"商品名称\",\"id\":\"spu_name\"}," +
                        "{\"name\":\"交易额\",\"id\":\"order_amount\"}," +
                        "{\"name\":\"订单数\",\"id\":\"order_count\"}]," +
                        "\"rows\":[");

        for (int i = 0; i < productStatsList.size(); i++) {
            ProductStats productStats = productStatsList.get(i);
            // 从第二个开始需要逗号
            if (i > 0){
                jsonBuilder.append(",");
            }
            jsonBuilder.append("{" +
                    "                        \"spu_name\": \""+productStats.getSpu_name()+"\"," +
                    "                        \"order_amount\": \""+productStats.getOrder_amount()+"\"," +
                    "                        \"order_count\":\""+productStats.getOrder_ct()+"\"" +
                    "                    }");
        }
        jsonBuilder.append("]}}");



        return jsonBuilder.toString();
    }
    /*
        {
            "status": 0,
            "data": [
                {
                    "name": "数码类",
                    "value": 371570
                },
                {
                    "name": "日用品",
                    "value": 296016
                }
            ]
        }
     */

    @RequestMapping("/category3")
    public String getCategory3(@RequestParam(name = "date",defaultValue = "0") Integer date){
        if (date == 0){
            date = now();
        }
        List<ProductStats> productStatsList = publisherService.getCategory3(date);

        // 封装json
        StringBuilder jsonBuilder = new StringBuilder("{" +
                "            \"status\": 0," +
                "            \"data\": [");
        for (int i = 0; i < productStatsList.size(); i++) {
            ProductStats productStats = productStatsList.get(i);
            if (i >= 1){
                jsonBuilder.append(",");
            }
            jsonBuilder.append("{" +
                    "                    \"name\": \""+productStats.getCategory3_name()+"\"," +
                    "                    \"value\": "+productStats.getOrder_amount()+"" +
                    "                }");
        }
        jsonBuilder.append("]" +
                "        }");
        return jsonBuilder.toString();
    }

}
